﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TDAPIOLELib;

namespace QCCure.Core
{
    [Serializable]
    public class QCBugFilterList : List<QCBugFilter>
    {
    }

    [Serializable]
    public class QCBugFilter : List<QCBugField>
    {
        private string name;

        public QCBugFilter()
        {
        }

        public QCBugFilter(string name)
        {
            this.name = name;
        }

        public QCBugFilter(TDFilter filter)
        {
            foreach (TDField field in filter.Fields)
            {
                this.Add(new QCBugField(field));
            }
        }

        public QCBugFilter(IList<QCBugField> fields)
        {
            this.AddRange(fields);
        }

        public QCBugFilter(string name, IList<QCBugField> fields)
        {
            this.name = name;
            this.AddRange(fields);
        }

        public QCBugFilter(string storedFilterString, QCController qc) 
        {
            FromStorableString(storedFilterString, qc);
        }

        public string ToStorableString(QCController qc)
        {
            string filterValue = qc.convertFilterToString(this);

            return "Name=" + Name + ";Value=" + filterValue;
        }

        public void FromStorableString(string storedFilterString, QCController qc)
        {
            string filterName = "[Custom Filter]";
            string filterValue = storedFilterString;

            if (storedFilterString.StartsWith("Name="))
            {
                string[] filterParts = storedFilterString.Split(new string[] { ";Value=" } , 2, StringSplitOptions.RemoveEmptyEntries);
                if (filterParts.Length >= 2)
                {
                    filterName = filterParts[0].Substring("Name=".Length);
                    filterValue = filterParts[1];
                }
            }

            this.name = filterName;
            qc.convertStringToFilter(this, filterValue);
        }

        public string Name
        {
            get {
                if (name == null)
                {
                    return "[Default]";
                } 
                return name;
            }
            set
            {
                this.name = value;
            }
        }

        // override object.Equals
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj)) {
                return true;
            }

            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            QCBugFilter other = obj as QCBugFilter;

            if (this.Count != other.Count) 
            {
                return false;
            }

            EqualityComparer<QCBugField> comparer = EqualityComparer<QCBugField>.Default;
            for (int i = 0; i < this.Count; i++)
            {
                if (!comparer.Equals(this[i], other[i])) 
                {
                    return false;
                }
            }

            return true;
        }

        // override object.GetHashCode
        public override int GetHashCode()
        {
            int hashCode = 0;

            for (int i = 0; i < this.Count; i++)
            {
                if (this[i] != null) {
                    hashCode = this[i].GetHashCode() & hashCode >> 32;
                }
            }
            return hashCode;
        }


        public override string ToString()
        {
            return Name;
        }

    }
}
